package com.wangshouyu.plugins.swagger;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;

/**
 * @author 王守钰
 * @date 2022-01-19 12:16
 * @description swagger版本配置，修改api文档
 */
public class VersionOperationBuilderPlugin implements OperationBuilderPlugin {
    @Override
    public void apply(OperationContext context) {
        Optional<ApiOperation> apiOperationOptional = context.findAnnotation(ApiOperation.class);
        Optional<ApiVersion> apiVersionOptional = context.findAnnotation(ApiVersion.class);
        if(apiOperationOptional.isPresent() && apiVersionOptional.isPresent()){
            ApiOperation apiOperation = apiOperationOptional.get();
            ApiVersion apiVersion = apiVersionOptional.get();
            OperationBuilder builder = context.operationBuilder()
                    .summary(apiOperation.value() + "(" + apiVersion.value() + "版本新增)");
            if(!Strings.isNullOrEmpty(apiVersion.description())){
                builder.notes(apiOperation.notes() + "("+apiVersion.description()+")");
            }
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
